package bb.lanxing.calc.processer;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.calc.data.CadencePoint;
import bb.lanxing.calc.data.DisplayPoint;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.util.CalorieUtil;
import bb.lanxing.util.Log;

public class BiCiSimpleWorkoutProcessor extends SimpleWorkoutProcessor {
    final static private String TAG = "BiCiSimpleWorkoutProcessor";
    @Override
    public void calcWorkout(SourcePoint sourcePoint) {
        int calcRealtimeCalorieWithSpeed;
        long duration = this.sourcePointPair.getDuration();
        Log.d(TAG, "calcWorkout duration=" + duration);
        if (duration < 1 || duration > 10) {
            duration = 1;
        }
        this.sourcePointPair.getDistance();
        CadencePoint cadencePoint = sourcePoint.getCadencePoint();
        if (cadencePoint != null) {
            this.displayPoint.setDuration((long) cadencePoint.getSumDuration());
            this.displayPoint.setDistance((double) cadencePoint.getSumDistance());
            this.displayPoint.setSpeed(sourcePoint.getSpeed());
            this.displayPoint.setCadence(cadencePoint.getCadence());
            cadencePoint.getSumDistance();
        }
        double speed = cadencePoint != null ? cadencePoint.getSpeed() : sourcePoint.getSpeed();
        double d = Utils.DOUBLE_EPSILON;
        double d2 = speed > Utils.DOUBLE_EPSILON ? speed * 3.6d : 0.0d;
        this.displayPoint.setSpeed(d2);
        if (d2 > this.displayPoint.getMaxSpeed(Utils.DOUBLE_EPSILON)) {
            this.displayPoint.setMaxSpeed(d2);
        }
        DisplayPoint displayPoint = this.displayPoint;
        if (this.displayPoint.getDuration(0L) > 0) {
            d = this.displayPoint.getDistance(Utils.DOUBLE_EPSILON) / this.displayPoint.getDuration(0L);
        }
        displayPoint.setAvgSpeed(d);
        Log.d(TAG, "calcWorkout AvgSpeed=" + d);
        if (sourcePoint.hasHeartrate()) {
            calcRealtimeCalorieWithSpeed = CalorieUtil.calcRealtimeCalorieWithHR(sourcePoint.getHeartratePoint().getHeartrate(), (int) duration);
        } else {
            calcRealtimeCalorieWithSpeed = CalorieUtil.calcRealtimeCalorieWithSpeed(sourcePoint.getSpeed(), (int) duration);
        }
        this.displayPoint.setCalorie(this.displayPoint.getCalorie(0) + calcRealtimeCalorieWithSpeed);
        this.elevationGradeCalc.calc(sourcePoint);
        float grade = this.elevationGradeCalc.getGrade();
        Log.v(TAG, "grade = " + grade);
        System.out.println("坡度(%): " + grade);
        this.displayPoint.setGrade(grade);
        float elevation = this.elevationGradeCalc.getElevation();
        Log.v(TAG, "elevation = " + elevation);
        if (elevation > 0.0f) {
            this.displayPoint.setElevationGain(this.displayPoint.getElevationGain(0.0f) + elevation);
        }
    }

    @Override
    public DisplayPoint process(SourcePoint sourcePoint) {
        if (checkPoint(sourcePoint)) {
            calcWorkout(sourcePoint);
        }
        return this.displayPoint;
    }
}
